﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaopxFramework.DAL
{
    public partial class Repository
    {
        public TaxonomyName InsertTaxonomyName(string name, bool active, bool isUseForPost, bool isUseForProduct)
        {
            var row = new TaxonomyName
                          {
                              Name = name,
                              Active = active,
                              IsUseForPost = isUseForPost,
                              IsUseForProduct = isUseForProduct,
                          };
            d.TaxonomyNames.InsertOnSubmit(row);
            Update();
            return row;
        }

        public TaxonomyName UpdateTaxonomyName(int id, string name, bool active, bool isUseForPost, bool isUseForProduct)
        {
            var row = GetTaxonomyName(id);
            if (row!=null)
            {
                row.Name = name;
                row.Active = active;
                row.IsUseForPost = isUseForPost;
                row.IsUseForProduct = isUseForProduct;
                Update();
            }
            return row;
        }

        public List<TaxonomyName> GetTaxonomyNames()
        {
            return d.TaxonomyNames.ToList();
        } 

        public TaxonomyName DeleteTaxonomyName(int id)
        {
            var row = GetTaxonomyName(id);
            d.TaxonomyNames.DeleteOnSubmit(row);
            Update();
            return row;
        }

        private TaxonomyName GetTaxonomyName(int id)
        {
            return d.TaxonomyNames.FirstOrDefault(i => i.Id == id);
        }
    }
}
